package flower.com.fenzu35011111.db;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.util.List;


import flower.com.fenzu35011111.bean.Optionals;


/**
 * Created by luoliwen on 16/4/28.
 * 完成对消息数据库的CRUD的操作
 */
public class Z_Optionals_Dao {
    private Context context;
    private Dao<Optionals, Integer> userDao;
    private Z_DataBaseHelper helper;

    public Z_Optionals_Dao(Context context) {
        this.context = context;
        helper = Z_DataBaseHelper.getInstance(context);
        try {
            userDao = helper.getDao(Optionals.class);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void addMSG(String Fenzu, String name, String Pinyin, String Gainian, String Industry, String Area


    ) {
        try {
            Optionals life_msg = new Optionals();
            life_msg.setId(String.valueOf(Math.random() * 10));
            life_msg.setFenzu(Fenzu);
            life_msg.setName(name);
            life_msg.setPinyin(Pinyin);
            life_msg.setGainian(Gainian);
            life_msg.setIndustry(Industry);
            life_msg.setArea(Area);
            life_msg.setZhiding("no");
            userDao.create(life_msg);

        } catch (Exception e) {
            e.printStackTrace();
            Log.e("88888", "" + e);
        }
    }

    List<Optionals> XTWUserInfos;

    public List<Optionals> query() {
        try {
            XTWUserInfos = userDao.queryForAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }

    public List<Optionals> query(String name) {
        try {
            if (name.equals("全部")) {
                XTWUserInfos = userDao.queryBuilder().orderBy("zhiding", false).selectColumns("name").distinct().query();
            } else {
                XTWUserInfos = userDao.queryBuilder().orderBy("zhiding", false).where().eq("fenzu", name).query();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }

    public List<Optionals> querycunzai(String name, String fenzu) {
        try {
            XTWUserInfos = userDao.queryBuilder().where().eq("name", name).and().eq("fenzu", fenzu).query();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return XTWUserInfos;
    }

    public void deleteUser(Optionals user) {
        try {
            userDao.delete(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void updata(Optionals optionals, String type) {
        try {
            UpdateBuilder<Optionals, Integer> life_devicesIntegerUpdateBuilder = userDao.updateBuilder();
            life_devicesIntegerUpdateBuilder.updateColumnValue("zhiding", type).

                    where().eq("id", optionals.getId());
            life_devicesIntegerUpdateBuilder.update();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
